package edu.task.snake.view;

import edu.task.snake.controller.GameEngine;
import edu.task.snake.model.Cell;
import edu.task.snake.model.Snake;

import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.util.ArrayList;

public class SnakeRender {

    Image head;
    Image body;
    Image tail;//ToDo
    ImageObserver observer;
    //    GameEngine observer;
    Snake snake;

    public SnakeRender(ImageObserver observer, Snake snake) {
        ImageIcon iia = new ImageIcon(this.getClass().getResource("head.png"));
        this.head = iia.getImage();
        iia = new ImageIcon(this.getClass().getResource("dot.png"));
        this.body = iia.getImage();
        this.observer = observer;
        this.snake = snake;
    }


    public Image getHead() {
        return head;
    }

    public Image getBody() {
        return body;
    }

    public Image getTail() {
        return tail;
    }


    public void render(Graphics g) {
        int scale = GameEngine.getCellSizePixel();

        ArrayList<Cell> cells = snake.getArrayList();
        int size = snake.getArrayList().size() - 1;
        Cell cell = snake.getArrayList().get(size);
        g.drawImage(head, cell.getX() * scale, cell.getY() * scale, scale, scale, observer);
        for (int i = 0; i <= size - 1; i++) {
            cell = cells.get(i);
            g.drawImage(body, cell.getX() * scale, cell.getY() * scale, scale, scale, observer);
        }

        Toolkit.getDefaultToolkit().sync();
    }
}
